package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.dto.TeacherBean;
import com.xh.bussiness.system.bean.request.TeacherRequest;
import com.xh.bussiness.system.dao.TeacherMapper;
import com.xh.bussiness.system.model.Teacher;
import com.xh.bussiness.system.service.ITeacherService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl extends BaseServiceImpl<TeacherMapper,Teacher> implements ITeacherService {
    @Override
    public Teacher getTeacherByUserId(String userId) {
        QueryWrapper qw=new QueryWrapper<Teacher>();
        qw.in("user_id",userId);
        return this.dao.selectOne(qw);
    }

    @Override
    public XhPage queryTeacherPage(XhPage page, TeacherRequest bean) {
        QueryWrapper qw=new QueryWrapper<Teacher>();
        //拼装条件
        qw=prepareParams(bean, qw);
        return (XhPage) this.selectPage(page,qw,TeacherBean.class);
    }

    @Override
    public List queryTeacher(TeacherRequest bean) {
        QueryWrapper qw=new QueryWrapper<Teacher>();
        //拼装条件
        qw=prepareParams(bean, qw);
        return this.selectList(qw, TeacherBean.class);
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(TeacherRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getUserId())) {
            qw.eq("user_id", bean.getUserId());
        }
        if (!StringUtils.isEmpty(bean.getName())) {
            qw.like("name", bean.getName());
        }
        return qw;
    }
}
